/*******************************************************************************
 * Copyright (c) 2007 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.ui.properties.tabbed;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.diagram.sequence.sedi.edit.AbstractLinkEditPart;
import com.cea.papyrus.diagram.sequence.sedi.edit.SeDiSubpartEditPart;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * 
 */
public class FontSection extends com.cea.papyrus.tabbedproperties.section.FontSection {

    /**
     * @param tabbedPropertySheetPage
     * @param parent
     * @see org.eclipse.ui.views.properties.tabbed.ITabbedPropertySection#createControls(org.eclipse.swt.widgets.Composite,
     *      org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
     */
    @Override
    public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
        super.createControls(parent, tabbedPropertySheetPage);

        // Replace button listener
        fontButton.removeSelectionListener(fontButtonListener);     
        fontButton.addSelectionListener(fontButtonListener = new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent event) {
                FontDialog ftDialog =
                        new FontDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                                .getShell());

                FontData oldFontdata = null;
                if (graphicalElement instanceof GraphNodeEditPart) {
                    oldFontdata =
                            new FontData(((GraphNodeWithSemanticEditPart) graphicalElement)
                                    .getGraphNode().getFontFamily(),
                                    ((GraphNodeWithSemanticEditPart) graphicalElement)
                                            .getGraphNode().getFontSize(), SWT.NORMAL);
                } else if (graphicalElement instanceof SeDiSubpartEditPart
                        || graphicalElement instanceof AbstractLinkEditPart) {
                    if (graphicalElement.getModel() instanceof GraphElement) {
                        GraphElement graphElement = (GraphElement) graphicalElement.getModel();
                        oldFontdata =
                                new FontData(graphElement.getFontFamily(), graphElement
                                        .getFontSize(), SWT.NORMAL);
                    }
                } else {
                    oldFontdata =
                            new FontData(((GraphEdgeEditPart) graphicalElement).getGraphEdge()
                                    .getFontFamily(), ((GraphEdgeEditPart) graphicalElement)
                                    .getGraphEdge().getFontSize(), SWT.NORMAL);

                }

                ftDialog.setFontData(oldFontdata);
                FontData fData = ftDialog.open();
                if (fData != null) {

                    if (graphicalElement instanceof GraphNodeEditPart) {
                        ((GraphNodeWithSemanticEditPart) graphicalElement).getGraphNode()
                                .setFontFamily(fData.getName());
                        ((GraphNodeWithSemanticEditPart) graphicalElement).getGraphNode()
                                .setFontSize(fData.getHeight());
                    } else if (graphicalElement instanceof SeDiSubpartEditPart
                            || graphicalElement instanceof AbstractLinkEditPart) {
                        if (graphicalElement.getModel() instanceof GraphElement) {
                            GraphElement graphElement = (GraphElement) graphicalElement.getModel();
                            graphElement.setFontFamily(fData.getName());
                            graphElement.setFontSize(fData.getHeight());
                        }
                    } else {// graphEdgeEditPart
                        ((GraphEdgeEditPart) graphicalElement).getGraphEdge().setFontFamily(
                                fData.getName());
                        ((GraphEdgeEditPart) graphicalElement).getGraphEdge().setFontSize(
                                fData.getHeight());

                    }

                    refresh();
                }
            }
        });
    }
}
